
layui.use(['form', 'table', 'eleTree', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form
    , table = layui.table
    , eleTree = layui.eleTree
    , fsoft = layui.fsoft;
  var tableId = "sysorg-tbs";
  var treeId = "fsoft-orgTree";
  var searchFormId = "fsoftSearchForm";
  // 节点点击事件
  eleTree.on("nodeClick(" + treeId + ")", function (d) {
    var currentData = d.data.currentData;
    $("#parents").val(currentData.id);
    mAction.doSearch();
  });
  //搜索监听
  form.on('submit(sreach)', function (data) {
    mAction.doSearch(data.field);
    return false;
  });
  // 工具栏监听 （批量操作）
  var mAction = {
    doSearch: function (params) { //查询
      //追加code、name查询条件
      if (params == undefined || params == null)
        params = $.extend({}, form.val(searchFormId));
      table.render({
        elem: '#' + tableId,
        title: '组织机构列表',
        toolbar: '#fsoft-table-toolbal',
        where: params,
        url: layui.cache['contentPath'] + '/sys-org/findList',
        cols: [[
          { type: 'checkbox', fixed: 'left', align: 'center', width: 60 },
          { type: 'numbers', fixed: 'left', title: '序号', align: 'center', width: 60 },
          { field: 'name', title: '单位名称', minWidth: 200, width: 250, align: 'center', style: 'text-align:left;' },
          { field: 'areaName', title: '所属地区', minWidth: 150, width: 300, align: 'center', style: 'text-align:left;' },
          { field: 'contactUser', title: '联系人', minWidth: 100, width: 120, align: 'center', style: 'text-align:left;' },
          { field: 'address', title: '地址', minWidth: 200, width: 300, align: 'center', style: 'text-align:left;' },
          { fixed: 'right', width: 105, align: 'center', title: '操作', toolbar: '#tbRowBar', style: 'text-align:left;' }
        ]],
        page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' },
        height: 'full-108'
      });
    }
    , removeBatch: function (data) {//批量删除	
      if (data == undefined || data.length == 0) {
        layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
        return;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要删除当前记录？";
      if (ids.length > 1)
        msg = "确认要删除已选中的（" + ids.length + "）条记录？";
      fsoft.confirm("询 问", msg, function (index) {
        fsoft.delete(layui.cache['contentPath'] + '/sys-org/remove', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("删除成功!");
            orgTree.reload();
          } else {
            fsoft.errorMsg("删除失败：" + rs.msg);
          }
          layer.close(index);
        });
      });
    }
    , showContentForm: function (data) {
      var path = layui.cache['contentPath'] + '/sys-org/add';
      var title = "添加组织机构";
      if (data) {
        title = data.name + '-编辑信息';
        path = layui.cache['contentPath'] + '/sys-org/edit/' + data.id;
      }
      layer.open({
        id: "sysorg-add-form"
        , type: 2
        , title: title
        , area: ['800px', '630px']
        , shadeClose: false
        , content: path
        , end: function () {
          orgTree.reload();
        }
      });
    }
  };

  //用户管理-组织机构树形-20191116
  var orgTree = eleTree.render({
    elem: '#' + treeId
    , method: 'post'
    , url: layui.cache['contentPath'] + '/sys-org/findOrgTrees'
    , request: { name: "title", key: "id", children: "children" }
    , highlightCurrent: true
    , defaultExpandAll: true
    , expandOnClickNode: false
    , showLine: true,
    done: function (res) {
      $("#parents").val(""); //树形没有默认选中节点
      if (res.code == 0) {
        var data = res.data;
        if (data != undefined && data.length > 0) {
          orgTree.expandNode(data[0].id);
        }
      }
      mAction.doSearch();
    }
  });

  /* 工具条（头部）监听 */
  table.on('toolbar(' + tableId + ')', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case 'add':
        mAction.showContentForm();
        break;
      case 'removeBatch':
        mAction.removeBatch(checkStatus.data);
        break;
    }
  });
  //行操作监听 
  table.on('tool(' + tableId + ')', function (obj) {
    var data = obj.data;
    if (obj.event === 'edit') {
      mAction.showContentForm(data);
    } else if (obj.event === 'del') {
      mAction.removeBatch([data]);
    }
  }); // End of 行监听 table.on
});